package com.byzk.p2p.admin.money.service;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.byzk.p2p.admin.base.util.BaseUtil;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.money.dao.FundRecordDao;
import com.byzk.p2p.admin.money.dto.FundRecord;
import com.byzk.p2p.admin.money.util.FundRecordType;
import com.byzk.p2p.admin.money.util.FundRecordTypeVo;
import com.byzk.p2p.admin.money.vo.FundRecordUserExportVo;
import com.byzk.p2p.admin.money.vo.FundRecordUserVo;
import com.byzk.p2p.admin.money.vo.SumMoneyVo;
import com.byzk.p2p.admin.user.dao.UserAccountDao;
import com.byzk.p2p.admin.user.dto.UserAccount;

/**
 * 资金流水service
 * @author suker.zhou
 *
 */
@Service
public class FundRecordService {
	
	@Autowired
	private FundRecordDao fundRecordDao;

	@Autowired
	private UserAccountDao userAccountDao;
	
	/**
	 * 资金流水新增修改
	 * @param fundRecord
	 * @return
	 */
	public Integer saveOrUpdate(FundRecord record){
		if(record.getId()==null){
			return fundRecordDao.insert(record);
		}else{
			return fundRecordDao.update(record);
		}
	}
	
	public Integer addFundRecord(Integer uid,Double income,Double expend,String terminal,String remark,Integer type,Double freezen){
		FundRecord record=new FundRecord();
		record.setCode(BaseUtil.generateCode(BaseUtil.ZIJINLIUSHUI));
		record.setCreateTime(new Date());
		record.setRemark(remark);
		record.setType(type);
		record.setUserId(uid);
		if(expend == 0.0){
			record.setIncome(income);
		}else{
			record.setIncome(0.00);
		}
		record.setExpend(expend);
		record.setTerminal(terminal);		
		UserAccount account=userAccountDao.getById(uid);	
		Double balance=account.getBalance();
		if(type==FundRecordType.Type.STAY_MONEY.index){
			record.setBalance(balance);	
		}else if (freezen == 0.0) {
			if(expend!=0.0){
				record.setBalance(income+balance-expend);
			}
			if(income!=0.0 && expend == 0.0){
				record.setBalance(balance+income);
			}
		}else{
			record.setBalance(balance);
		}
		record.setFreezen(freezen);
		return fundRecordDao.insert(record);
	}
	
	/**
	 * 资金流水记录
	 * @param limit
	 * @param index
	 * @param record
	 * @return
	 */
	public PageInfo<FundRecordUserVo> list(FundRecordUserVo vo){
		int count=fundRecordDao.count(vo);
		List<FundRecordUserVo> recordList=fundRecordDao.list(vo);
		SumMoneyVo sumMoneyVo = fundRecordDao.sumFundRecord(vo);
		PageInfo<FundRecordUserVo> page=new PageInfo<FundRecordUserVo>(count, vo.getIndex() , vo.getLimit());
		if(sumMoneyVo == null){
			page.setSumBlance(0.0);
			page.setSumFee(0.0);
		}else{
			page.setSumBlance(sumMoneyVo.getFund());
			page.setSumFee(sumMoneyVo.getInterest());
		}
		page.setPageList(recordList);
		return page;
	}
	
	/**
	 * 资金流水详情
	 * @param id
	 * @return
	 */
	public FundRecordUserVo detail(Integer id){
		FundRecordUserVo vo=fundRecordDao.selectByPrimaryKey(id);
		return vo;
	}
	
	/**
	 * 获取某种资金流水数量
	 * @param vo
	 * @return
	 */
	public int getFundRecordCount(FundRecordUserVo vo){
		return fundRecordDao.count(vo);
	}
	
	/**
	 * 获取某种资金流水
	 * @param vo
	 * @return
	 */
	public FundRecordUserVo getFundRecord(FundRecordUserVo vo){
		return fundRecordDao.getFundRecord(vo);
	}
	
	/**
	 * 导出资金流水
	 * @param ids
	 * @return
	 */
	public List<FundRecordUserVo> exportList(FundRecordUserExportVo vo){
		return fundRecordDao.listByExportVo(vo);
	}
	
	public FundRecord selectFundRecordByUserId(Integer id){
		return fundRecordDao.getFundRecordByUserId(id);
	}
	
	public Integer addFundRecordCoupon(Integer uid,Double income,Double expend,String terminal,String remark,Integer type,Double freezen){
		UserAccount account = userAccountDao.getByUserId(uid);
		FundRecord recordVo=new FundRecord();
		recordVo.setCode(BaseUtil.generateCode(BaseUtil.ZIJINLIUSHUI));
		recordVo.setIncome(income);
		recordVo.setExpend(0.00);
		recordVo.setType(type);
		recordVo.setUserId(uid);
		recordVo.setRemark(remark);
		recordVo.setCreateTime(new Date());
		recordVo.setBalance(account.getBalance());
		recordVo.setFreezen(0.00);
		return fundRecordDao.insert(recordVo);
		
	} 
	
	public List<FundRecord> listByTypeAndAmount(Integer type,double amount,Integer uid){
		return fundRecordDao.listByTypeAndAmount(type,amount,uid);
	}
	
	public List<FundRecordUserVo> getDrawRecordList() {
		FundRecordUserVo vo = new FundRecordUserVo();
		vo.setType(507);
		vo.setStart(0);
		vo.setLimit(6);
		
//		vo.setRemark("1");
		return fundRecordDao.list(vo);
	}
	
	/**
	 * 获取该用户邀请获得的奖励总和
	 * @param userId 用户id
	 * @return
	 */
	public Double sumInviteReward(Integer userId) {
		return fundRecordDao.sumInviteReward(userId);
	}
}